package com.nidu.demo.log.consumer;

import com.nidu.demo.auth.model.LoginEvent;
import com.nidu.demo.log.gateway.LoginLogGateway;
import com.nidu.demo.log.model.LoginLog;
import com.nidu.demo.user.gateway.UserGateway;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 登录事件处理器
 *
 * @author auto
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class LoginEventHandler {

    private final LoginLogGateway loginLogGateway;

    private final UserGateway userGateway;

    @Async("loginExecutor")
    @EventListener
    public void handleLogin(LoginEvent event) {

        LoginLog loginLog = event.getLoginLog();

        userGateway.updateLoginInfo(loginLog.getUserId(), loginLog.getUserIp());

        loginLogGateway.create(loginLog);
    }
}
